<template>
  <div>
    <!-- 顶部面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 头部警告 -->
      <el-alert
        title="注意：只允许为第三级分类设置相关参数！"
        type="warning"
        :closable="false"
        show-icon
      >
      </el-alert>
      <!-- 选择商品分类 -->
      <el-row class="cat_opt">
        <el-col>
          <span>选择商品分类:</span>
          <!-- 级联选择框 -->
          <el-cascader
            :options="cateLists"
            v-model="selectCatekeys"
            :props="cateProps"
            @change="handleChange"
          >
          </el-cascader>
        </el-col>
      </el-row>
      <!-- Tabs区 -->
      <el-tabs v-model="activeName" @tab-click="handleTabClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button :disabled="isBtnDisabled" type="primary" size="mini" @click="addDialogVisible = true">添加{{buttonText}}</el-button>
          <!-- 动态参数表格 -->
          <el-table :data="formData" border stripe>
            <!-- 展开行 -->
            <el-table-column type="expand">
              <!-- 循环渲染tag标签 -->
              <template v-slot="scope">
                <el-tag
                  v-for="(item, i) in scope.row.attr_vals"
                  :key="i"
                  closable
                  @close="deleteTag(i, scope.row)"
                >
                  {{item}}
                </el-tag>
                <!-- tag标签输入文本框 -->
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  clearable
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                >
                </el-input>
                <el-button v-else class="button-new-tag" size="small" @click="showInput(scope.row)">+ New Tag</el-button>
              </template>
            </el-table-column>
            <el-table-column type="index"></el-table-column>
            <el-table-column label="参数名称" prop="attr_name"></el-table-column>
            <el-table-column label="操作">
              <template v-slot="scope">
                <el-button type="primary" size="mini" icon="el-icon-edit" @click="editAttr(scope.row)">编辑</el-button>
                <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteAttr(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <el-button :disabled="isBtnDisabled" type="primary" size="mini" @click="addDialogVisible = true">添加属性</el-button>
          <!-- 静态属性表格 -->
          <el-table :data="formData" border stripe>
            <!-- 展开行 -->
            <el-table-column type="expand">
              <!-- 循环渲染tag标签 -->
              <template v-slot="scope">
                <el-tag
                  v-for="(item, i) in scope.row.attr_vals"
                  :key="i"
                  closable
                  @close="deleteTag(i, scope.row)"
                >
                  {{item}}
                </el-tag>
                <!-- tag标签输入文本框 -->
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  clearable
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                >
                </el-input>
                <el-button v-else class="button-new-tag" size="small" @click="showInput(scope.row)">+ New Tag</el-button>
              </template>
            </el-table-column>
            <el-table-column type="index"></el-table-column>
            <el-table-column label="属性名称" prop="attr_name"></el-table-column>
            <el-table-column label="操作">
              <template v-slot="scope">
                <el-button type="primary" size="mini" icon="el-icon-edit" @click="editAttr(scope.row)">编辑</el-button>
                <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteAttr(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 添加参数对话框 -->
    <el-dialog
      :title="titleTexts"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <!-- 对话框输入表单 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="170px">
        <el-form-item :label="titleTexts" prop="attr_name">
          <el-input v-model="addForm.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addFormAttr">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      addDialogVisible: false,
      cateLists: [],
      //* 级联选择框的配置对象
      cateProps: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      //* 级联选择框双向绑定的数组
      selectCatekeys: [],
      activeName: 'many',
      formData: [],
      //* 添加参数、属性表单
      addForm: {
        attr_name: ''
      },
      addFormRules: {
        attr_name: [
          { required: true, message: '请输入', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.$http.getCateList()
      .then(resp => {
        // console.log(resp)
        this.cateLists = resp.data.data
      })
  },
  computed: {
    //* 按钮禁用true,否则false
    isBtnDisabled () {
      return this.selectCatekeys.length !== 3
    },
    //* 当前选中的三级分类的ID
    // cateID = this.selectCatekeys[this.selectCatekeys.length - 1]
    cateId () {
      if (this.selectCatekeys.length === 3) {
        return this.selectCatekeys[2]
      }
      return null
    },
    //* 添加框标题
    titleTexts () {
      return this.activeName === 'many' ? '动态参数' : '静态属性'
    },
    buttonText () {
      return this.activeName === 'many' ? '参数' : '属性'
    }
  },
  methods: {
    //* 级联选择框触发事件
    handleChange () {
      const keyLength = this.selectCatekeys.length
      if (keyLength !== 3) {
        this.selectCatekeys = []
        this.formData = []
        // return
      } else {
        // console.log(this.selectCatekeys)
        //* 发送对应静态动态数据请求
        // console.log(this.cateId)
        this.$http.getAttributes(this.cateId, this.activeName)
          .then(resp => {
            resp.data.data.forEach(element => {
              element.attr_vals = element.attr_vals ? element.attr_vals.split(' ') : []
              element.inputVisible = false
              element.inputValue = ''
              //* 增加一个控制tag文本输入框的属性
            })
            this.formData = resp.data.data
            // console.log(resp.data.data)
          })
      }
    },
    //* Tab页签点击事件
    handleTabClick () {
      // console.log(this.activeName)
      this.handleChange()
    },
    //* 添加参数属性对话框关闭
    addDialogClosed () {
      this.$refs.addFormRef.resetFields()
    },
    //* 弹窗 确认添加和编辑属性参数事件
    addFormAttr () {
      // console.log(this.cateId, this.addForm.attr_name, this.activeName)
      if (this.addForm.attr_name) {
        //  ^属性参数弹窗编辑确定事件
        // console.log(123)
        this.$http.putCategoriesAttr(
          this.addForm.cat_id,
          this.addForm.attr_id,
          this.addForm.attr_name,
          this.addForm.attr_sel,
          this.addForm.attr_vals
        ).then(resp => {
          // console.log(resp)
          this.$message.success(resp.data.meta.msg)
          this.addDialogVisible = false
          this.handleChange()
        })
      } else {
        //  ^属性参数弹窗添加确定事件
        this.$refs.addFormRef.validate(valid => {
          if (!valid) return
          this.$http.postAttributes(this.cateId, this.addForm.attr_name, this.activeName)
            .then(resp => {
              // console.log(resp)
              this.$message.success('添加成功')
              this.addDialogVisible = false
              this.handleChange()
            })
        })
      }
    },
    //* 编辑参数属性 按钮
    editAttr (row) {
      // console.log(row)
      this.addForm.attr_name = row.attr_name
      this.addForm = {
        ...this.addForm,
        ...row
      }
      // console.log(this.addForm)
      this.addDialogVisible = true
    },
    //* 删除参数属性
    deleteAttr (row) {
      this.$confirm('此操作将删除此商品分类, 是否继续?', '删除角色', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.deleteCategoriesAttr(row.cat_id, row.attr_id)
          .then(resp => {
            this.handleChange()
          })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        // console.log(id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    //* 文本框输入点击事件
    handleInputConfirm (row) {
      //* 用户没有输入
      if (row.inputValue.trim().length === 0) {
        row.inputValue = ''
        row.inputVisible = false
        return
      }
      //* 用户输入内容
      row.attr_vals.push(row.inputValue.trim())
      row.inputValue = ''
      row.inputVisible = false
      // console.log(row)
      this.changTagText(row)
    },
    //* tag数据更改
    changTagText (row) {
      this.$http.putCategoriesAttr(
        row.cat_id,
        row.attr_id,
        row.attr_name,
        row.attr_sel,
        row.attr_vals.join(' ')
      ).then(resp => {
        // console.log(resp)
        this.$message.success(resp.data.meta.msg)
      })
    },
    //* tag文本框点击变输入框
    showInput (row) {
      row.inputVisible = true
      // console.log(row)
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    //* 删除tag
    deleteTag (i, row) {
      // console.log(i)
      row.attr_vals.splice(i, 1)
      this.changTagText(row)
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-card {
    margin-top: 15px;
    .cat_opt {margin: 15px 0;}
    .el-tag {margin: 10px;}
    .input-new-tag {
      margin: 10px;
      width: 150px;
    }
    .el-cascader-menu {
      height: 240px;
    }
  }
</style>
